// pages/refund_all/refund_all.js
var app = getApp();
var ApiUrl = app.globalData.ApiUrl;
var order_id = "";
var order_goods_id = "";
 
var key = "";
Page({
 
  data: {

  },
  onLoad: function (options) {
    key = wx.getStorageSync("key");
    order_id = options.order_id;
    order_goods_id = options.order_goods_id;
    var that = this;
    wx.request({
      url: ApiUrl + '/index.php?act=member_refund&op=refund_form',
      data: { "key": key, "order_id": order_id, "order_goods_id": order_goods_id },
      header: {
        'content-type': "application/x-www-form-urlencoded"
      },
      success: function (result) {
         
        var reason_list = result.data.datas.reason_list;
        var reasonlen = 0;
        var jsonArr = [];
        for (var i in reason_list) {
          jsonArr[reasonlen] = reason_list[i];
          reasonlen++;
        }
        that.setData({
          order: result.data.datas.order,
          goods: result.data.datas.goods,
          reason_list: jsonArr,
          key: key,
          order_id: order_id,
          info: "请选择"
        });
      }
    });
  },
  actionSheetTap: function (e) {

    var that = this;
    var reason_id = that.data.reason_list[e.detail.value]['reason_id'];
    var info = that.data.reason_list[e.detail.value]['reason_info'];
    that.setData({
      reason_id: reason_id,
      info: info
    });
  },
  chooseImage: function (options) {
    var filekey = options.currentTarget.dataset.filekey;
    var that = this;
    wx.chooseImage({
      count: 3,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        wx.uploadFile({
          url: ApiUrl+"index.php?act=member_refund&op=upload_pic",
          filePath: tempFilePaths[0],
          name: 'refund_pic',
          formData: {
            'key': key,
          },
          success: function (result) {
            var datas = JSON.parse(result.data);
            var picname = new Array();
            var file_name = new Array();
            picname[filekey] = datas.datas.pic;
            file_name[filekey] = datas.datas.file_name
            that.setData({
              pic: picname,
              file_name: file_name
            });
          }
        })
      }

    });
  },
  formSubmit: function (params) {

    var that = this;
    var paramArr = {};
    paramArr = params.detail.value;

    paramArr.key = that.data.key;
    paramArr.order_id = that.data.order_id;
    paramArr.refund_type = 2;
     
    if (!paramArr.buyer_message) {
      wx.showToast({
        title: '请填写退货说明',
        icon: "loading",
        mask: true,
        duration: 1000,
      });
      return false;
    } else {
      
      wx.request({
        url: ApiUrl + '/index.php?act=member_refund&op=refund_post',
        data: paramArr,
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        method: 'post',
        success: function (result) {

          if (result.data.code == "200") {
            
            wx.reLaunch({
              url: '/pages/order/order',
            })
            
          }
        }
      })
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})